/**
 * 设置页面头部菜单<br>
 * 请保证1、2的顺序，其它随意
 */
$(function(){
	// 1、生成菜单
	buildHeadMenu();
	
	// 2、绑定导航菜单事件
	bindNavEvent();
	
	// n、渲染已登录用户信息
	renderLogin();
});

/**
 * 生成菜单
 */
function buildHeadMenu(){
    var headMenuHtml = [];
	
	headMenuHtml.push('<nav class="navbar navbar-default navbar-fixed-top navbar-app">');
	headMenuHtml.push('<div class="container">');
	headMenuHtml.push('<div class="col-lg-12">');
	headMenuHtml.push('<div class="navbar-header">');
	headMenuHtml.push('<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">');
	headMenuHtml.push('<span class="sr-only">&nbsp;</span>');
	headMenuHtml.push('<span class="icon-bar"></span>');
	headMenuHtml.push('<span class="icon-bar"></span>');
	headMenuHtml.push('<span class="icon-bar"></span>');
	headMenuHtml.push('</button>');
	headMenuHtml.push('<a class="navbar-brand" id="homePage" href="#"> <!-- <img src="/images/steer-logo.png" alt="Steer logo"> -->MyStyle</a>');
	headMenuHtml.push('</div>');
	
	headMenuHtml.push('<div class="navbar-collapse navbar-right collapse" aria-expanded="false" style="height: 1px;">');
	// 菜单
	headMenuHtml.push('<ul class="nav navbar-nav" id="nav_menu">');
	headMenuHtml.push('<li><a href="#" id="nav_accounting">'+$.i18n.prop("accounts")+'</a></li>');
	headMenuHtml.push('<li><a href="#" id="nav_discovery">'+$.i18n.prop("discovery")+'</a></li>');
	headMenuHtml.push('<li><a href="#" id="nav_about">'+$.i18n.prop("about")+'</a></li>');
	headMenuHtml.push('<li><a href="#" id="nav_login">'+$.i18n.prop("login")+'</a></li>');
	headMenuHtml.push('<li><a href="#" id="nav_loginout" style="display: none;">'+$.i18n.prop("login_out")+'</a></li>');
	headMenuHtml.push('</ul>');
	
	headMenuHtml.push('</div>');
	headMenuHtml.push('</div>');
	headMenuHtml.push('</div>');
	headMenuHtml.push('</nav>');
	
	$("body").prepend(headMenuHtml.join(" "));
}

/**
 * 绑定导航菜单事件
 */
function bindNavEvent(){
	// 菜单
	$("#homePage").attr("href",rootPath+"/portal/home.html");
	$("#nav_accounting").attr("href",rootPath+"/portal/accounting/account.html");
	$("#nav_discovery").attr("href",rootPath+"/portal/accounting/discovery.html");
	$("#nav_about").attr("href",rootPath+"/portal/common/about.html");
	$("#nav_login").attr("href",rootPath+"/portal/common/login.html");
	$("#nav_loginout").click(function(){
		// 请求后台注销当前会话，不显示遮罩层
		var param = {data:{}, url:"/user/loginout", isLoading:false, callback:function(response){
			if(response.code == 0){
				sessionStorage.clear();
				// 重新渲染
				renderLogin();
			}
		}};
		$.myajax(param);
	});
}

/**
 * 渲染已登录用户信息
 */
function renderLogin(){
	if(sessionStorage.getItem("userSession")){
		$("#nav_login").hide();
		$("#nav_loginout").show();
		
		var userSession = JSON.parse(sessionStorage.getItem("userSession"));
		var ulel = [];
		ulel.push('<li><a href="#" id="nav_userinfo">');
		ulel.push(userSession.name);
		ulel.push('</a></li>');
		
		$("#nav_loginout").parent().before(ulel.join(" "));
	}else{
		// 用户信息移除
		$("#nav_userinfo").remove();
		// 未登录，显示登录
		$("#nav_login").show();
		// 未登录，隐藏注销
		$("#nav_loginout").hide();
	}
}